slf.getUser();
var buyModel = {};
var isBuy=false;
var courseId='';
slf.insertSuccessCode='';
var updateHtml='';
var updateDOM='';
var coursePageIds='';
buyModel.buyCoursePage=function(){
	var operate = arguments[0] || {};
	//点击事件绑定
	var money=operate.money||'';
	courseId=operate.courseId;
	coursePageIds=operate.coursePageIds;
	var childMess=slf.getSequenceChild();
	var childrenId=childMess.childrenId;
	console.log(childrenId)
	console.log("孩子id")
	if(slf.schoolId==undefined||slf.schoolId==null||slf.schoolId=='a'){
		var messPrompt="请先完善孩子信息，是否现在去完善？"
		$("#class_model .am-modal-bd").html(messPrompt)
		$(".zcc").addClass("am-hide")
		$("#class_model").modal({
			closeViaDimmer: false,
			onConfirm: function(options) {
				window.location.href = "personal/child_list.html"
	        },
		});
	}else{
		buyModel.orders(money,2,childrenId);
	}
	updateHtml=operate.htmlmodel||'';
	updateDOM=operate.updateDOM||'';
}
//购买课程包 几种状态
buyModel.orders=function (money,x,childrenId) {
	console.log(money+","+childrenId)
	var html = "";
	$('#class_model .am-modal-hd').html("购买课程包");
	switch(x) {
		case 0: //在课程关系链中没有前置关系的情况
			html += "<ul class='am-avg-sm-1 am-text-left font14'>";
			html += "<li>您已经有进行中的系统学习类课程，但与即将购买的课程不冲突，请在确认自身上课时间重组后再购买</li>";
			html += "<li>是否花费<span class='text-FF2A2A'>1800</span>(原价2000)个立方币购买本课程包?<li>"
			html += "</ul>";
			$("#class_model .am-modal-bd").html(html);
			$('#class_model').modal({
				relatedTarget: this,
				closeViaDimmer: false,
				onConfirm: function(options) {
					$("#class_model").removeData('amui.modal');
					console.log("在课程关系链中没有前置关系的情况,购买课程结束")
				}
			});
			break;
		case 1: //课程包关系链中有前置关系的情况
			html += "<ul class='am-avg-sm-1 am-text-left font14'>";
			html += "<li>您已经有进行中的系统学习类课程，购买当前课程包后，会在您上完本课程包的所有课程后自动为您排班上课</li>";
			html += "<li>是否花费<span class='text-FF2A2A'>1800</span>(原价2000)个立方币购买本课程包?<li>"
			html += "</ul>";
			$("#class_model .am-modal-bd").html(html);
			$('#class_model').modal({
				relatedTarget: this,
				closeViaDimmer: false,
				onConfirm: function(options) {
					$("#class_model").removeData('amui.modal');
					console.log("课程包关系链中有前置关系的情况,购买课程结束");
					resmodel.resmodel(0); //购买成功 失败
				}
			});
			break;
		case 2: //正常购买
			html += "<ul class='am-avg-sm-1 am-text-left font14'>";
			html += "<li>是否花费<span class='text-FF2A2A'>"+money+"</span>个立方币购买本课程包?<li>"
			html += "</ul>";
			$("#class_model .am-modal-bd").html(html);
			var i=0;
			var $confirm = $('#class_model');
            var confirm = $confirm.data('amui.modal');
            $(".zcc").addClass("am-hide")
            var onConfirm = function() {
            	i++;
				if(i==1){
					$("#class_model").removeData('amui.modal');
					buyModel.insertCoursePage(childrenId);
				}else{
					return;
				}
            };
            var onCancel = function() {
                
            }
            if (confirm) {
              confirm.options.onConfirm =  onConfirm;
              confirm.options.onCancel =  onCancel;
              confirm.toggle(this);
            } else {
              $confirm.modal({
                relatedElement: this,
                closeViaDimmer: false,
                onConfirm: onConfirm,
                onCancel: onCancel
              });
            }
			break;
	}
}

//添加课程包
buyModel.insertCoursePage=function insertCoursePage(childrenId){
	if(slf.getQueryStringByName('coursePage')==undefined||slf.getQueryStringByName('coursePage')==null||slf.getQueryStringByName('coursePage')==''){
		coursePageIds=courseId;
	}
	slf.ajax({
		url:'/myCoursePage/insertMyCoursePage',
		param:{
			courseId:coursePageIds,
			childrenId:childrenId
		},
		success:function(data){
			console.log(data)
			console.log("判断是否购买")
			var course = data.obj;
			//TODO 后台异常获取抛出有误
			if(data.res != 1){
				if(data.resMsg=="余额不足。"){
					$("#buyMoneyModal").modal({
						relatedTarget: this,
						closeViaDimmer: false,
				        onConfirm: function(options) {
				        	window.location.href = "recharge.html"; //去充值页面
				        },
					});
				}else{
					$("#alert1 .am-modal-bd").html("<span>"+data.resMsg+"</span>");
					$('#alert1').modal("open");
				}
			}else{
				$("#alert1 .am-modal-bd").html("<span>"+data.resMsg+"</span>");
				$('#alert1').modal({
					relatedTarget: this,
					closeViaDimmer: false,
			        onConfirm: function(options) {
			        	location.reload();
						if(updateHtml!=null&&updateHtml!=''){
							updateDOM.html(updateHtml);
						}
			        },
				});
			}
		}
	});
}